package com.llkj.qingnianguoxue.adapter;

import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.bean.BookVideoBean;
import com.llkj.qingnianguoxue.bean.BookVideoListBean;
import com.llkj.qingnianguoxue.bean.CoinExchangeBean;
import com.llkj.qingnianguoxue.util.DisplayUtil;
import com.llkj.qingnianguoxue.util.ImageLoadingUtil;
import com.llkj.qingnianguoxue.util.StringUtils;
import com.llkj.qingnianguoxue.util.TextViewUtil;
import com.xiaweizi.cornerslibrary.CornersProperty;

import java.util.List;

/**
 * 范儿币兑换适配器
 */
public class CoinExchangeAdapter extends BaseQuickAdapter<CoinExchangeBean, BaseViewHolder> {

    private ExchangeItemClick mItemClick;

    public CoinExchangeAdapter(int layoutResId, List<CoinExchangeBean> data, ExchangeItemClick listener) {
        super(layoutResId, data);
        mItemClick = listener;
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, CoinExchangeBean categoryBean) {
        baseViewHolder.setText(R.id.tv_category_name,categoryBean.getCategoryName());

        int categoryType = categoryBean.getCategoryType();
        switch (categoryType){
            case 1:
                initVideoItem(baseViewHolder,categoryBean);
                break;
            case 2:
                initBookItem(baseViewHolder,categoryBean);
                break;
            case 3:
                initMasterItem(baseViewHolder,categoryBean);
                break;
            case 4:
                initCourseItem(baseViewHolder,categoryBean);
                break;
            case 5:
                initActionItem(baseViewHolder,categoryBean);
                break;
        }

    }

    /**
     * 视频分类
     */
    private void initVideoItem(BaseViewHolder baseViewHolder, CoinExchangeBean categoryBean){
        LinearLayout llItems = baseViewHolder.getView(R.id.llItems);
        baseViewHolder.addOnClickListener(R.id.tv_more);
        llItems.removeAllViews();
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View itemView = null;
        BookVideoBean item = null;
        int size = categoryBean.getCategoryList().size();
        size = size>3?3:size;
        for(int i=0;i<size;i++){
            itemView = inflater.inflate(R.layout.item_jing_xuan_vedio2,null);
            llItems.addView(itemView);
            item = categoryBean.getCategoryList().get(i);

            ImageView ivPic = (ImageView) itemView.findViewById(R.id.iv_video_pic);
            ImageLoadingUtil.loadImgGlideRoundCenterCrop(ivPic, item.getImg());
            TextView tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvTitle.setText(item.getTitle());
            LinearLayout.LayoutParams titleParam = (LinearLayout.LayoutParams) tvTitle.getLayoutParams();
            titleParam.width = ViewGroup.LayoutParams.MATCH_PARENT;

            TextView tvPrice = (TextView) itemView.findViewById(R.id.tv_price);
            tvPrice.setGravity(Gravity.LEFT);
            tvPrice.setText("￥" + item.getPrice());
            TextView tvCatoryTag = (TextView) itemView.findViewById(R.id.tvTag);
            if(!StringUtils.isEmpty(item.getClassify())){
                tvCatoryTag.setText(item.getClassify());
                tvCatoryTag.setVisibility(View.VISIBLE);
            }else{
                tvCatoryTag.setVisibility(View.INVISIBLE);
            }

            FrameLayout.LayoutParams picParams = (FrameLayout.LayoutParams) ivPic.getLayoutParams();
            tvTitle.setMaxLines(1);
            picParams.width = (DisplayUtil.getDensityWdith(mContext)-DisplayUtil.dip2px(mContext, 50))/3;
            picParams.height = picParams.width*9/16;

            setTtemMargin(itemView,i,picParams.width);

            setItemListener(itemView,1,item);
        }
    }
    /**
     * 书籍分类
     */
    private void initBookItem(BaseViewHolder baseViewHolder, CoinExchangeBean categoryBean){
        LinearLayout llItems = baseViewHolder.getView(R.id.llItems);
        baseViewHolder.addOnClickListener(R.id.tv_more);
        llItems.removeAllViews();
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View itemView = null;
        BookVideoBean item = null;
        int size = categoryBean.getCategoryList().size();
        size = size>4?4:size;
        for(int i=0;i<size;i++){
            itemView = inflater.inflate(R.layout.item_jing_xuan_vedio2,null);
            llItems.addView(itemView);
            item = categoryBean.getCategoryList().get(i);

            ImageView ivVideoPlay = (ImageView) itemView.findViewById(R.id.iv_play);
            ivVideoPlay.setVisibility(View.INVISIBLE);
            ImageView ivPic = (ImageView) itemView.findViewById(R.id.iv_video_pic);
            ImageLoadingUtil.loadImgGlideRoundCenterCrop(ivPic, item.getImg());
            TextView tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvTitle.setText(item.getTitle());
            LinearLayout.LayoutParams titleParam = (LinearLayout.LayoutParams) tvTitle.getLayoutParams();
            titleParam.width = ViewGroup.LayoutParams.MATCH_PARENT;

            TextView tvPrice = (TextView) itemView.findViewById(R.id.tv_price);
            tvPrice.setText("￥" + item.getPrice());
            tvPrice.setGravity(Gravity.LEFT);
            TextView tvCatoryTag = (TextView) itemView.findViewById(R.id.tvTag);
            if(!StringUtils.isEmpty(item.getBooksclassify())){
                tvCatoryTag.setText(item.getBooksclassify());
                tvCatoryTag.setVisibility(View.VISIBLE);
            }else{
                tvCatoryTag.setVisibility(View.INVISIBLE);
            }

            FrameLayout.LayoutParams picParams = (FrameLayout.LayoutParams) ivPic.getLayoutParams();
            tvTitle.setMaxLines(1);
            picParams.width = (DisplayUtil.getDensityWdith(mContext)-DisplayUtil.dip2px(mContext, 60))/4;
            picParams.height =(int)(picParams.width*14/13);

            setTtemMargin(itemView,i,picParams.width);

            setItemListener(itemView,2,item);
        }
    }

    /**
     * 导师分类
     */
    private void initMasterItem(BaseViewHolder baseViewHolder, CoinExchangeBean categoryBean){
        LinearLayout llItems = baseViewHolder.getView(R.id.llItems);
        baseViewHolder.addOnClickListener(R.id.tv_more);
        llItems.removeAllViews();
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View itemView = null;
        BookVideoBean item = null;
        int size = categoryBean.getCategoryList().size();
        size = size>4?4:size;
        for(int i=0;i<size;i++){
            itemView = inflater.inflate(R.layout.item_jing_xuan_vedio2,null);
            llItems.addView(itemView);
            item = categoryBean.getCategoryList().get(i);

            ImageView ivVideoPlay = (ImageView) itemView.findViewById(R.id.iv_play);
            ivVideoPlay.setVisibility(View.INVISIBLE);
            ImageView ivPic = (ImageView) itemView.findViewById(R.id.iv_video_pic);
            ImageLoadingUtil.loadImgGlideRoundCenterCrop(ivPic, item.getImg());
            TextView tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvTitle.setText(item.getName());
            LinearLayout.LayoutParams titleParam = (LinearLayout.LayoutParams) tvTitle.getLayoutParams();
            titleParam.width = ViewGroup.LayoutParams.MATCH_PARENT;

            TextView tvPrice = (TextView) itemView.findViewById(R.id.tv_price);
            tvPrice.setText(item.getPrice()+"元/月");
            tvPrice.setGravity(Gravity.LEFT);
            TextView tvCatoryTag = (TextView) itemView.findViewById(R.id.tvTag);
            tvCatoryTag.setVisibility(View.INVISIBLE);

            FrameLayout.LayoutParams picParams = (FrameLayout.LayoutParams) ivPic.getLayoutParams();
            tvTitle.setMaxLines(1);
            picParams.width = (DisplayUtil.getDensityWdith(mContext)-DisplayUtil.dip2px(mContext, 60))/4;
            picParams.height =(int)(picParams.width*9/16);


            setTtemMargin(itemView,i,picParams.width);

            setItemListener(itemView,3,item);
        }
    }

    /**
     * 课程分类
     * @param baseViewHolder
     * @param categoryBean
     */
    private void initCourseItem(BaseViewHolder baseViewHolder, CoinExchangeBean categoryBean){
        LinearLayout llItems = baseViewHolder.getView(R.id.llItems);
        baseViewHolder.addOnClickListener(R.id.tv_more);
        llItems.removeAllViews();
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View itemView = null;
        BookVideoBean item = null;
        int size = categoryBean.getCategoryList().size();
        size = size>2?2:size;
        for(int i=0;i<size;i++) {
            itemView = inflater.inflate(R.layout.item_jing_xuan_action, null);
            llItems.addView(itemView);
            item = categoryBean.getCategoryList().get(i);

            ImageView ivPic = (ImageView) itemView.findViewById(R.id.iv_pic);
            ImageLoadingUtil.loadImgGlideRound(ivPic, item.getImg(),
                    5, CornersProperty.CornerType.TOP);
            TextView tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvTitle.setText(item.getName());

            String money = "￥" + item.getPrice()+"/时";
            TextView tvPrice = (TextView) itemView.findViewById(R.id.tv_price);
            SpannableString strPrice = TextViewUtil.getForegroundColorSpan(mContext,money,money.length()-2,money.length(), ContextCompat.getColor(mContext, R.color.color999999));
            tvPrice.setText(strPrice);
            TextView tvDate = (TextView) itemView.findViewById(R.id.tv_date);
            String time = item.getStart_time();
            if(!StringUtils.isEmpty(time)){
                try {
                    time = time.substring(0,10);
                }catch (Exception e){}
            }
            tvDate.setText(time);

            TextView tvState = (TextView) itemView.findViewById(R.id.tv_state);//预约状态
            tvState.setTextSize(12);
            boolean isReserve = !"3".equals(item.getIs_reserve())?true:false;
            tvState.setSelected(isReserve);
            if("1".equals(item.getIs_reserve())){
                tvState.setText("已预约");
            }else if("2".equals(item.getIs_reserve())){
                tvState.setText("满额");
            }else{
                tvState.setText("预约");
            }

            RelativeLayout.LayoutParams picParams = (RelativeLayout.LayoutParams) ivPic.getLayoutParams();
            picParams.width = (DisplayUtil.getDensityWdith(mContext)-DisplayUtil.dip2px(mContext, 40))/2;
            picParams.height = picParams.width/2;
            setTtemMargin(itemView,i,picParams.width);

            setItemListener(itemView,4,item);
        }
    }

    /**
     * 活动分类
     * @param baseViewHolder
     * @param categoryBean
     */
    private void initActionItem(BaseViewHolder baseViewHolder, CoinExchangeBean categoryBean){
        LinearLayout llItems = baseViewHolder.getView(R.id.llItems);
        baseViewHolder.addOnClickListener(R.id.tv_more);
        llItems.removeAllViews();
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View itemView = null;
        BookVideoBean item = null;
        int size = categoryBean.getCategoryList().size();
        size = size>2?2:size;
        for(int i=0;i<size;i++) {
            itemView = inflater.inflate(R.layout.item_exchange_action, null);
            llItems.addView(itemView);
            item = categoryBean.getCategoryList().get(i);

            ImageView ivPic = (ImageView) itemView.findViewById(R.id.iv_action_pic);
            ImageLoadingUtil.loadImgGlideRound(ivPic, item.getImg(),
                    5, CornersProperty.CornerType.TOP);
            TextView tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvTitle.setText(item.getName());
            TextView tvAdress = (TextView) itemView.findViewById(R.id.tv_address);
            tvAdress.setText(item.getSite());

            String money = "￥" + item.getPrice();
            TextView tvPrice = (TextView) itemView.findViewById(R.id.tv_money);
            tvPrice.setText(money);

            TextView tvState = (TextView) itemView.findViewById(R.id.tv_state);//预约状态
            boolean isReserve = "1".equals(item.getIs_reserve())?true:false;
            tvState.setSelected(isReserve);

            LinearLayout.LayoutParams picParams = (LinearLayout.LayoutParams) ivPic.getLayoutParams();
            picParams.width = (DisplayUtil.getDensityWdith(mContext)-DisplayUtil.dip2px(mContext, 40))/2;
            picParams.height = picParams.width/2;
            setTtemMargin(itemView,i,picParams.width);

            setItemListener(itemView,5,item);
        }
    }

    private void setItemListener(View itemView, final int type, final BookVideoBean item){
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mItemClick!=null){
                    mItemClick.exchangeClick(type,item);
                }
            }
        });
    }

    private void setTtemMargin(View itemView, int position, int itemWidth){
        LinearLayout.LayoutParams rootParams = (LinearLayout.LayoutParams) itemView.getLayoutParams();
        rootParams.width = itemWidth;
        if(position==0){
            rootParams.leftMargin = DisplayUtil.dip2px(mContext,15);
        }else{
            rootParams.leftMargin = DisplayUtil.dip2px(mContext,10);
        }
    }

    public interface ExchangeItemClick{
        void exchangeClick(int type, BookVideoBean item);
    }
}
